﻿/* $RCSFile: CrmGenClass.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/CrmGenClass/CrmGenClass.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:19  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:49  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:24  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;  
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Globalization;

using AddonNice.Core;
using AddonNice.UI;
using AddonNice.Design;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;

using AddonNice.CRMWrapper;
using CrmSdk;
using CrmSdk.CRM4;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;
using System.IO;


namespace AddonNice.PageModules.CrmGenClass
{
    /// <summary>
    /// Initializer class for announcements 
    /// </summary>
    public class CrmGenClassSI : PortalCRMModuleControlSI
    {
        public CrmGenClassSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CrmGenClassSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CrmGenClassSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CrmGenClassSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]CrmGenClassSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    public partial class CrmGenClass : PortalCRMModuleControl
    {

        #region General implementation

		/// <summary>
		/// Only 4
		/// </summary>
		public override VersionType ModuleType
		{
			get
			{
				return VersionType.CrmV3;
			}
		}

        public override bool AdminModule
        {
            get
            {
                return true;
            }
        }

        public override Guid GuidID
        {
            get
            {
                return new Guid("{53D86D68-160A-454a-B356-DFDF2FA267EA}");
            }
        }
        #endregion General implementation

        /// <summary>
        /// No entity needed here
        /// </summary>
        public override bool NeedAttachedEntity
        {
            get
            {
                return false;
            }
        }

        ArrayList _AllEntities =   null;
        ArrayList GetAllEntities(bool useAll)
        {
            if (_AllEntities == null)
            {
                _AllEntities          =   MetadataCache.GetAllEntities(Wrapper,useAll);
            }
            return _AllEntities;
        }
        
        /// <summary>
        /// constructor installs validation group 'Main'
        /// </summary>
        public CrmGenClass()
        {
            ValidationGroup                     =   "Main";
        }
        
        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new CrmGenClassSI(bSettings,this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmGenClass Page_Load BEG IsPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
   			// If first visit to the page, update all entries
			if ( !IsPostBack ) 
			{
                // Select by default Web Page
                ArrayList are                               =   GetAllEntities(true);
                RadComboEntities.DataTextField              =   "displayName";
                RadComboEntities.DataValueField             =   "entityName";
                RadComboEntities.Sort                       =   RadComboBoxSort.Ascending;
                RadComboEntities.SortCaseSensitive          =   false;
                RadComboEntities.DataSource                 =   are;
                DataBind();
                RadComboEntities.DataBind();
                RadComboEntities.SortItems();
			}
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmGenClass Page_Load END IsPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
        }


        void RadComboEntities_SelectedIndexChanged(object o,RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string entDisplayName           =   e.Text;
            string entName                  =   e.Value;
            EntityGenerator gen             =   new EntityGenerator(Wrapper,entName);
            Literal lit                     =   new Literal();
            string result                   =   gen.CreateClass();
            File.WriteAllText(string.Format("\\{0}.cs",gen.className),result);
            lit.Text                        =   result.Replace("\r\n","<br />");
            PnSummary.Controls.Add(lit);
        }


        #region Std Overrides
 
        public override void Page_PreInit(object sender, EventArgs e)
        {
            base.Page_PreInit(sender, e);
            string strcult      =   Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            Load                                +=  new System.EventHandler(Page_Load);
            RadComboEntities.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(RadComboEntities_SelectedIndexChanged);
            RadComboEntities.AutoPostBack       =   true;
        }

        #endregion
    }
}